package com.isdepci.project.carbon.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.isdepci.common.utils.StringUtils;
import com.isdepci.project.carbon.domain.ProductCarbon;
import com.isdepci.project.carbon.mapper.ProductCarbonMapper;
import com.isdepci.project.carbon.service.IProductCarbonStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.project.carbon.domain.ProductCarbonConsumables;
import com.isdepci.project.carbon.service.IProductCarbonConsumablesService;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.framework.web.page.TableDataInfo;

/**
 * 产品碳足迹-产品排放行为记录Controller
 *
 * @author isdepci
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/carbon/productCarbonConsumables")
public class ProductCarbonConsumablesController extends BaseController {
    @Autowired
    private IProductCarbonConsumablesService productCarbonConsumablesService;
    @Autowired
    private IProductCarbonStageService productCarbonStageService;
    @Autowired
    private ProductCarbonMapper productCarbonMapper;

    /**
     * 求当前产品的平均值
     */
    @GetMapping(value = "avg/{productId}")
    public AjaxResult selectAvgByProductId(@PathVariable("productId") Long productId) {
        BigDecimal avg = productCarbonConsumablesService.selectAvgByProductId(productId);
        return AjaxResult.success(avg);
    }
    /**
     * 查询减排路径
     *    产品名称、碳排放行为相同
     *    碳排放因子低于当前因子数值
     *    从小到大排列
     */
    @GetMapping("/emission/{id}")
    public AjaxResult selectEmissionPath(@PathVariable("id") Long id) {
        return productCarbonConsumablesService.selectEmissionPath(id);
    }
    @PostMapping("/emissionList")
    public TableDataInfo emissionList(@RequestBody ProductCarbonConsumables productCarbonConsumables) {
        ProductCarbonConsumables old = productCarbonConsumablesService.selectProductCarbonConsumablesById(productCarbonConsumables.getId());
        if(old==null){
            return  getDataTable(new ArrayList<>());
        }
        if(StringUtils.isEmpty(old.getCategorizeCode())){
            return  getDataTable(new ArrayList<>());
        }
        //查询所有相同阶段的ID
        List<Long> stageIds = productCarbonStageService.selectStageIdsByStageId(old.getStageId());
        if(stageIds.size()<=0){
            return  getDataTable(new ArrayList<>());
        }
        productCarbonConsumables.setStageIds(stageIds);
        productCarbonConsumables.setCategorizeCode(old.getCategorizeCode().split(",")[0]);
        productCarbonConsumables.setFactorNum(old.getFactorNum());
        startPage();
        List<ProductCarbonConsumables> list = productCarbonConsumablesService.selectEmissionPathList(productCarbonConsumables);
        if(list.size()>0){
            List<ProductCarbon> carbons = productCarbonMapper.selectListByIds(list.stream().map(ProductCarbonConsumables::getProductCarbonId).collect(Collectors.toList()));
            for (ProductCarbonConsumables consumables : list) {
                ProductCarbon carbon = carbons.stream().filter(v-> v.getId().equals(consumables.getProductCarbonId())).findFirst().orElse(null);
                if(carbon!=null){
                    consumables.setEnterpriseId(carbon.getEnterpriseId());
                    consumables.setProductBatch(carbon.getProductBatch());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 采纳按钮 汇总至“减排路径记录”
     */
    @PostMapping("/summary")
    public AjaxResult summaryEmissionPathLog(@RequestBody ProductCarbonConsumables productCarbonConsumables) {
        if(productCarbonConsumables.getId()==null||productCarbonConsumables.getStageIds()==null||productCarbonConsumables.getStageIds().size()<=0){
            return AjaxResult.error("参数异常");
        }
        return productCarbonConsumablesService.summaryEmissionPathLog(productCarbonConsumables.getId(),productCarbonConsumables.getStageIds());
    }

    /**
     * 查询产品碳足迹-产品排放行为记录列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProductCarbonConsumables productCarbonConsumables) {
        startPage();
        List<ProductCarbonConsumables> list = productCarbonConsumablesService.selectProductCarbonConsumablesList(productCarbonConsumables);
        return getDataTable(list);
    }




    /**
     * 获取产品碳足迹-产品排放行为记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productCarbonConsumablesService.selectProductCarbonConsumablesById(id));
    }

    /**
     * 新增产品碳足迹-产品排放行为记录
     */
    @Log(title = "产品碳足迹-产品排放行为记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCarbonConsumables productCarbonConsumables) {
        return toAjax(productCarbonConsumablesService.insertProductCarbonConsumables(productCarbonConsumables));
    }

    /**
     * 修改产品碳足迹-产品排放行为记录
     */
    @Log(title = "产品碳足迹-产品排放行为记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCarbonConsumables productCarbonConsumables) {
        return toAjax(productCarbonConsumablesService.updateProductCarbonConsumables(productCarbonConsumables));
    }

    /**
     * 删除产品碳足迹-产品排放行为记录
     */
    @Log(title = "产品碳足迹-产品排放行为记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productCarbonConsumablesService.deleteProductCarbonConsumablesByIds(ids));
    }
}
